﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Data;
using FirstFloor.ModernUI.Windows;
using FirstFloor.ModernUI.Windows.Controls;
using System.Threading;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarCliente.xaml
    /// </summary>
    public partial class ModificarCliente : UserControl , IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public ModificarCliente()
        {
            InitializeComponent();
        }

        private async void cargarClientes()
        {
            this.pb.IsIndeterminate = true;

            this.dgModificarClientes.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarClientesTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            this.dgModificarClientes.ItemsSource = slowTask.Result.DefaultView;

            this.dgModificarClientes.Visibility = System.Windows.Visibility.Visible;

            this.pb.IsIndeterminate = false;
        }

        private DataTable cargarClientesTask()
        {
            return this.persistencia.obternerClientes();      
        }

        private void dgModificarClientes_Row_DoubleClick(object sender, RoutedEventArgs e)
        {
            if (this.dgModificarClientes.SelectedItem != null)
            {
                Cliente cliente = new Cliente();

                try
                {
                    cliente.id_cliente = int.Parse(((DataRowView)this.dgModificarClientes.SelectedItem).Row["id_cliente"].ToString());
                    cliente.nombre = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["nombre"].ToString();
                    cliente.rut = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["rut"].ToString();
                    cliente.giro = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["giro"].ToString();
                    cliente.ciudad = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["ciudad"].ToString();
                    cliente.direccion = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["direccion"].ToString();
                    cliente.comuna = ((DataRowView)this.dgModificarClientes.SelectedItem).Row["comuna"].ToString();

                    ModificarClienteDialog mcd = new ModificarClienteDialog(cliente);

                    var wnd = new ModernWindow
                    {
                        Style = (Style)App.Current.Resources["EmptyWindow"],
                        Content = mcd,
                        Width = 500,
                        Height = 300,
                        Margin = new Thickness(42),
                        Padding = new Thickness(42),
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        ResizeMode = ResizeMode.NoResize
                    };

                    wnd.ShowDialog();

                    this.cargarClientes();
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            this.cargarClientes();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            
        }
    }
}
